import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import AdminLayout from '@/views/AdminLayout.vue'
import Dashboard from '@/views/Dashboard.vue'
import Products from '@/views/Products.vue'
import Brands from '@/views/Brands.vue'
import Categories from '@/views/Categories.vue'
import Colors from '@/views/Colors.vue'
import Sizes from '@/views/Sizes.vue'
import Users from '@/views/Users.vue'
import PendingOrders from '@/views/orders/PendingOrders.vue'
import ShippingOrders from '@/views/orders/ShippingOrders.vue'
import CompletedOrders from '@/views/orders/CompletedOrders.vue'
import ReturnRequestOrders from '@/views/orders/ReturnRequestOrders.vue'
import ReturnedOrders from '@/views/orders/ReturnedOrders.vue'
import CanceledOrders from '@/views/orders/CanceledOrders.vue'
import PerformanceReports from '@/views/reports/PerformanceReports.vue'
import MarketingReports from '@/views/reports/MarketingReports.vue'
import TrafficReports from '@/views/reports/TrafficReports.vue'
import CompanyLinks from '@/views/company/CompanyLinks.vue'
import CompanyAds from '@/views/company/CompanyAds.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/admin',
    component: AdminLayout,
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: Dashboard
      },
      {
        path: 'products',
        name: 'ProductsList',
        component: Products
      },
      {
        path: 'brands',
        name: 'BrandsList',
        component: Brands
      },
      {
        path: 'categories',
        name: 'CategoriesList',
        component: Categories
      },
      {
        path: 'colors',
        name: 'ColorsList',
        component: Colors
      },
      {
        path: 'sizes',
        name: 'SizesList',
        component: Sizes
      },
      {
        path: 'users',
        name: 'UsersList',
        component: Users
      },
      {
        path: 'orders/pending',
        name: 'PendingOrders',
        component: PendingOrders
      },
      {
        path: 'orders/shipping',
        name: 'ShippingOrders',
        component: ShippingOrders
      },
      {
        path: 'orders/completed',
        name: 'CompletedOrders',
        component: CompletedOrders
      },
      {
        path: 'orders/return-request',
        name: 'ReturnRequestOrders',
        component: ReturnRequestOrders
      },
      {
        path: 'orders/returned',
        name: 'ReturnedOrders',
        component: ReturnedOrders
      },
      {
        path: 'orders/canceled',
        name: 'CanceledOrders',
        component: CanceledOrders
      },
      {
        path: 'reports/performance',
        name: 'PerformanceReports',
        component: PerformanceReports
      },
      {
        path: 'reports/marketing',
        name: 'MarketingReports',
        component: MarketingReports
      },
      {
        path: 'reports/traffic',
        name: 'TrafficReports',
        component: TrafficReports
      },
      {
        path: 'company/links',
        name: 'CompanyLinks',
        component: CompanyLinks
      },
      {
        path: 'company/ads',
        name: 'CompanyAds',
        component: CompanyAds
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  
  // 如果访问登录页面且已经登录，跳转到后台管理页面
  if (to.path === '/login' && isLoggedIn) {
    next('/admin/dashboard')
    return
  }
  
  // 如果访问后台管理页面但未登录，跳转到登录页面
  if (to.path.startsWith('/admin') && !isLoggedIn) {
    next('/login')
    return
  }
  
  next()
})

export default router 